@file: JvmName("ViewExt")

package com.polaris.live.common.ext

import android.content.res.Resources
import android.os.Build
import android.view.View
import androidx.core.widget.NestedScrollView
import com.polaris.live.common.appContext
import me.jessyan.autosize.utils.AutoSizeUtils

/**
 * view常用的方法，封装成扩展函数
 *
 * @author Created by 半仙 on 2023/7/31/031 17:57
 */
fun View.setVisibleOrGone(isShow: Boolean) {
    visibility = if (isShow) View.VISIBLE
    else View.GONE
}

fun View.setVisibleOrHide(isShow: Boolean) {
    visibility = if (isShow) View.VISIBLE
    else View.INVISIBLE
}

/**
 * 将传入的Int类型转换为需要的像素值
 */
val Int.dp: Int
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_DIP,
        this.toFloat(),
        Resources.getSystem().displayMetrics
    ).toInt()

/**
 * 将传入的Int类型转换为需要的像素值
 */
val Int.autoDp: Int
    get() = AutoSizeUtils.dp2px(appContext, this.toFloat())
val Int.autoSp: Int
    get() = AutoSizeUtils.sp2px(appContext, this.toFloat())

/**
 * 将传入的Float类型转换为需要的像素值
 */
val Float.dp: Float
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics
    )

/**
 * 将传入的Float类型转换为需要的像素值
 */
val Float.sp: Float
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics
    )

/**
 * 将传入的Float类型转换为需要的像素值
 */
val Int.sp: Int
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_SP,
        this.toFloat(),
        Resources.getSystem().displayMetrics
    ).toInt()

abstract class OnSingleClickListener(val duration: Int) : View.OnClickListener {

    private var lastClickTime = -1L

    final override fun onClick(view: View) {
        lastClickTime = if (lastClickTime < 0) {
            onSingleClick(view)
            System.currentTimeMillis()
        } else {
            val now = System.currentTimeMillis()
            if (now - lastClickTime > duration) {
                onSingleClick(view)
            }

            now
        }
    }

    /**
     * 点击操作回调。
     *
     * @param view 当前点击操作的视图
     */
    abstract fun onSingleClick(view: View)
}

/**
 * 设置单次点击事件监听器，避免连续快速点击造成两次重复操作。
 *
 * @param duration 点击时间间隔
 * @param onSingleClick 点击事件回调，快速连续点击将被忽略掉
 */
fun View.setOnSingleClickListener(duration: Int = 500, onSingleClick: (view: View) -> Unit) {
    setOnClickListener(object : OnSingleClickListener(duration) {
        override fun onSingleClick(view: View) {
            onSingleClick.invoke(view)
        }
    })
}
fun NestedScrollView.setOnScrollChangeListenerCompat(onScrollChange: (scrollY: Int) -> Unit) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        setOnScrollChangeListener { _, _, scrollY, _, _ ->
            onScrollChange(scrollY)
        }
    } else {
        viewTreeObserver.addOnScrollChangedListener {
            onScrollChange(scrollY)
        }
    }
}